﻿// -----------------------------------------------------------------------
// <copyright file="PartInEquipment_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using PMSGSC.Common;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class PartInEquipment_DAL: BaseDAL
    {

        // Lấy tất cả PartsInEquipment
        public static List<PartsInEquipment> GetAll(int equipmentID)
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.PartsInEquipments);
            return DB.PartsInEquipments.Where(n => n.Equipment.Equipment_ID == equipmentID).ToList();
        }

        public static List<PartsInEquipment> GetAllByShip(int ShipID)
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.PartsInEquipments);
            return DB.PartsInEquipments.Where(n => n.Equipment.Ship_ID == ShipID).ToList();
        }
        // Lấy PartsInEquipment theo Id
        public static PartsInEquipment GetDataById(PartsInEquipment jp)
        {
            return DB.PartsInEquipments.Where(m => (m.Parts_ID == jp.Parts_ID) && (m.Equipment_ID == jp.Equipment_ID)).FirstOrDefault();
        }
        public static PartsInEquipment GetDataById(int id)
        {
            return DB.PartsInEquipments.Where(e => e.Parts_ID == id).FirstOrDefault();
        }
        public static PartsInEquipment GetDataByNumber(string number)
        {
            return DB.PartsInEquipments.Where(e => e.PartCode == number).FirstOrDefault();
        }
        public static PartsInEquipment GetDataByName(string name)
        {
            return DB.PartsInEquipments.Where(e => e.PartName == name).FirstOrDefault();
        }
        //Thêm một PartsInEquipment
        public static bool Insert(PartsInEquipment PartsInEquipment)
        {
            try
            {
                DB.PartsInEquipments.InsertOnSubmit(PartsInEquipment);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        // Xóa một PartsInEquipment
        public static bool Delete(PartsInEquipment PartsInEquipment)
        {
            try
            {
                if (DB.JobParts.Where(j => j.Parts_ID == PartsInEquipment.Parts_ID).FirstOrDefault() == null)
                {

                    DB.PartsInEquipments.DeleteOnSubmit(PartsInEquipment);
                    DB.SubmitChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }

        }
        public static bool DeleteByID(PartsInEquipment jp)
        {
            PartsInEquipment objPartsInEquipment = GetDataById(jp);
            return Delete(objPartsInEquipment);
        }
        // Cập nhật PartsInEquipment
        public static bool Update(PartsInEquipment PartsInEquipment)
        {
            try
            {
                PartsInEquipment objPartsInEquipment = GetDataById(PartsInEquipment);
                objPartsInEquipment.Equipment_ID = PartsInEquipment.Equipment_ID;
                objPartsInEquipment.Parts_ID = PartsInEquipment.Parts_ID;
                objPartsInEquipment.DrawingNo = PartsInEquipment.DrawingNo;
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        public static bool isExits(PartsInEquipment jp)
        {
            PartsInEquipment objPartJob = null;
            objPartJob = DB.PartsInEquipments.Where(j => ((j.Equipment_ID == jp.Equipment_ID) && (j.PartCode == jp.PartCode) && (j.Parts_ID != jp.Parts_ID))).FirstOrDefault();
            return objPartJob != null ? true : false;
        }
        public static bool TestBeforeDelete(PartsInEquipment part)
        {
            LocationInStock location = DB.LocationInStocks.Where(n => n.Parts_ID == part.Parts_ID).FirstOrDefault();
            if (location != null)
                return true;

            JobPart partJob= DB.JobParts.Where(n => n.Parts_ID == part.Parts_ID).FirstOrDefault();
            if (partJob != null)
                return true;

            PartWorkOrder partWO = DB.PartWorkOrders.Where(n => n.Parts_ID == part.Parts_ID).FirstOrDefault();
            if (partWO != null)
                return true;

            InvoiceDetail invoiceDetail = DB.InvoiceDetails.Where(n => n.Part_ID == part.Parts_ID).FirstOrDefault();
            if (invoiceDetail != null)
                return true;


            return false;

        }
        public static List<PartsInEquipment> GetDataByEquipmentID(int id, bool isChangedA)
        {
            if (isChangedA)
            {
                DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.PartsInEquipments);
            }
            return DB.PartsInEquipments.Where(j => j.Equipment_ID == id).ToList();
        }
        public static List<PartsInEquipment> FilterStockItem(PartsInEquipment part, int StockId, int ShipID)
        {
            var predicate = PredicateBuilder.True<PartsInEquipment>();
            predicate = predicate.And(n => n.Equipment.Ship_ID == ShipID);
            if (part.PartCode != "")
                predicate = predicate.And(n => n.PartCode == part.PartCode);
            if (part.PartName != "" && part.PartName[0] == '$' && part.PartName[part.PartName.Length - 1] == '$')
            {
                part.PartName = part.PartName.Split('$')[1];
                predicate = predicate.And(n => n.PartName.ToLower().Contains(part.PartName.ToLower()));
            }
            else
            {
                if (part.PartName != "")
                    predicate = predicate.And(n => n.PartName.ToLower().CompareTo(part.PartName.ToLower()) == 0);
            }

            if(part.Equipment_ID != 0)
                predicate = predicate.And(n => n.Equipment_ID == part.Equipment_ID);
            if (part.Maker_ID != 0)
                predicate = predicate.And(n => n.Maker_ID == part.Maker_ID);
            if (part.Vendor_ID != 0)
                predicate = predicate.And(n => n.Vendor_ID == part.Vendor_ID);
            if (StockId != 0)
                predicate = predicate.And(n => n.LocationInStocks.Where(m => m.Stock_ID == StockId).FirstOrDefault() != null);
            if (part.DrawingNo != "")
                predicate = predicate.And(n => n.DrawingNo == part.DrawingNo);

            return DB.PartsInEquipments.Where(predicate).Cast<PartsInEquipment>().ToList();

        }
    }
}
